package czx.wt.controller;

import czx.wt.util.MockDatabase;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * @author ChenZhiXiang
 * @version 1.0.0
 * @Description TODO
 * @createTime 2018年12月27日 23:08:00
 */
@Controller
public class SsoServerController {

    private static final Logger logger = LoggerFactory.getLogger(SsoServerController.class);

    /**
     * 检查是否有全局会话.
     * @param redirectUrl
     * @param session
     * @return
     */
    @GetMapping("/checkLogin")
    public String checkLogin(@RequestParam("redirectUrl") String redirectUrl,
                             HttpSession session){
        logger.info("判断是否有全局的会话"+redirectUrl);
        logger.info("从会话中获取令牌信息,如果取不到说明没有全局会话,如果能取到说明有全局会话");
        String token = (String) session.getAttribute("token");
        logger.info("token"+token);
        if (StringUtils.isEmpty(token)){
            logger.info("没有全局会话");
            //跳转到统一认证中心的登录页面
            session.setAttribute("redirectUrl",redirectUrl);
            return "login";
        } else {
            logger.info("有全局会话"+redirectUrl);
            session.setAttribute("token",token);
            return "redirect:"+redirectUrl+"?token"+token;
        }
    }

    /**
     * 统一登录接口
     * @param username
     * @param password
     * @param redirectUrl
     * @param request
     * @return
     */
    @PostMapping("/login")
    public String login(String username, String password,
                        String redirectUrl, HttpServletRequest request, Model model){
        if (username.equals("czx") && password.equals("123456")){
            logger.info("账号密码匹配"+redirectUrl);
            logger.info("创建令牌信息");
            redirectUrl = (String) request.getSession().getAttribute("redirectUrl");
            String token = UUID.randomUUID().toString();
            logger.info("创建的UUID:"+token);
            logger.info("创建全局会话，将令牌信息放入全局会话中");
            HttpSession session = request.getSession();
            session.setAttribute("token",token);
            logger.info("将令牌放入数据库中");
            MockDatabase.T_TOKEN.add(token);
            StringBuilder url = new StringBuilder(50);
            model.addAttribute("token",token);
            return "redirect:"+redirectUrl+"?token="+token;
        }
        logger.info("账号密码错误");
        return "login";
    }

    @PostMapping("/verify")
    @ResponseBody
    public String verifyToken(String token){
        logger.info("在模拟数据库中查找是否有该token");
        if (MockDatabase.T_TOKEN.contains(token)){
            return "true";
        } else {
            return "false";
        }
    }

}
